using System;
using Common.Commands.MemberCommands;
using Common.Enumerations;
using Common.Views;
using NUnit.Framework;
using Sanelib.Common.Extensions;
using Sanelib.Common.Helpers;
using Sanelib.DataView;
using Sanelib.Dto.Enumerations;

namespace IntegrationTests.Processors
{
    public class MemberProcessorTester : IntegrationTestBase
    {
        [Test]
        public void ShouldAddMember()
        {
            var command = new AddMember
            {
                Id = GuidComb.New(),
                Code = "Admin",
                Name = "Admin",
                TagId = "tagid",
                DateOfBirth = DateTime.Now.AddYears(-20),
                Address = "adminAddress",
                Email = "sunny@sanelib.com",
                Mobile = "9099791517",
                ScreenMessage = "Hi, Admin",
                ValidFrom = DateTime.Now.AddMonths(-1),
                ValidTo = DateTime.Now.AddMonths(2),
                SendSms = true,
                SendEmail = true,
                UsePrint = true,
                Block = false,
                Role = Role.ApplicationOwner,
                UserStatus = UserStatus.Active,
                ImageData = ImageUtility.NoImageData
            };

            ProcessCommand(command);

            var repo = GetInstance<IViewRepository<MemberView>>();
            var member = repo.GetByKey(Property.Of<MemberView>(x => x.Id), command.Id);
            Assert.AreEqual(member.Name, command.Name);
        }

        [Test, Explicit]
        public void ShouldChangePassword()
        {
            var member = GetSiteUser("test", "test");
            Persist(member);
            var command = new ChangePassword
            {
                Id = member.Id,
                OldPassword = member.PasswordHash,
                NewPassword = "$Admin123",
                ConfirmPassword = "$Admin123"
            };
            ProcessCommand(command);

            var repo = GetInstance<IViewRepository<MemberView>>();
            var memberView = repo.GetByKey(Property.Of<MemberView>(x => x.Id), command.Id);

            Assert.AreNotEqual(memberView.PasswordHash, member.PasswordHash);
        }

        [Test]
        public void ShouldDeleteMember()
        {
            var member = GetSiteUser();
            Persist(member);

            var command = new DeleteMember(member.Id);
            ProcessCommand(command);
            var repo = GetInstance<IViewRepository<MemberView>>();
            var memberView = repo.GetById(member.Id);
            Assert.IsNull(memberView);
        }

        [Test]
        public void ShouldEditMember()
        {
            var member = GetSiteUser("test", "test");
            Persist(member);

            var command = new UpdateMember
            {
                Id = member.Id,
                Code = member.Code,
                TagId = member.TagId,
                Name = "Admin",
                DateOfBirth = DateTime.Now.AddYears(-22),
                Address = "Baroda",
                Email = "vishal123@gmail.com",
                Mobile = "9601303333",
                ValidFrom = DateTime.Now.AddMonths(-1),
                ValidTo = DateTime.Now.AddMonths(2),
                SendSms = true,
                SendEmail = true,
                UsePrint = true,
                Block = true,
                Role = Role.ApplicationOwner,
                UserStatus = UserStatus.Active,
                ImageData = ImageUtility.NoImageData,
                ScreenMessage = "Welcome to Sanelib Demo"
            };
            ProcessCommand(command);

            var repo = GetInstance<IViewRepository<MemberView>>();
            var memberrepo = repo.GetByKey(Property.Of<MemberView>(x => x.Id), command.Id);
            Assert.AreEqual(command.Address, memberrepo.Address);
        }

        [Test]
        public void ShouldRegisterMemberCard()
        {
            var member = GetSiteUser();
            Persist(member);

            var command = new RegisterMemberCard
            {
                Id = member.Id,
                TagId = "ABCDEF"
            };

            ProcessCommand(command);
        }

        [Test]
        public void ShouldUpdateNotification()
        {
            var member = GetSiteUser();
            Persist(member);

            var commandumn = new UpdateMemberNotification
            {
                Id = member.Id,
                Email = "foo@ab.com",
                Mobile = "foo",
                Block = true
            };

            ProcessCommand(commandumn);

            var repo = GetInstance<IViewRepository<MemberView>>();
            var memberView = repo.GetById(member.Id);

            Assert.AreNotEqual(memberView.Email, member.Email);
            Assert.AreNotEqual(memberView.Block, member.Block);
        }
    }
}